package com.zimi.service;

import cn.hutool.core.date.DateTime;
import com.zimi.mapper.SysOrgExtendMapper;
import com.zimi.mapper.WfSystaskMapper;
import com.zimi.mapper.WfSystaskVOMapper;
import com.zimi.model.SysOrgExtend;
import com.zimi.model.WfSystask;
import com.zimi.model.WfSystaskVO;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class WfSystaskService {
    @Autowired
    private WfSystaskMapper wfSystaskMapper;
    @Autowired
    private WfSystaskVOMapper wfSystaskVOMapper;
    @Autowired
    private SysOrgExtendMapper sysOrgExtendMapper;


    public int deleteByPrimaryKey(Integer id) {
        return wfSystaskMapper.deleteByPrimaryKey(id);
    }


    public int insert(WfSystask record) {
        return wfSystaskMapper.insert(record);
    }


    public int insertSelective(WfSystask record) {
        return wfSystaskMapper.insertSelective(record);
    }


    public WfSystask selectByPrimaryKey(Integer id) {
        return wfSystaskMapper.selectByPrimaryKey(id);
    }


    public int updateByPrimaryKeySelective(WfSystask record) {
        return wfSystaskMapper.updateByPrimaryKeySelective(record);
    }


    public int updateByPrimaryKey(WfSystask record) {
        return wfSystaskMapper.updateByPrimaryKey(record);
    }


    public List<WfSystask> selectAll(WfSystask record) {
        return wfSystaskMapper.selectAll(record);
    }

    //自动插入创建时间
    public WfSystask autoCreateTime(WfSystask record) {
        DateTime now = new DateTime();

        if (record.getCreateTime() == null) {
            record.setCreateTime(now);
        }

        return record;
    }

    //自动插入更新时间
    public WfSystask autoUpdateTime(WfSystask record) {
        DateTime now = new DateTime();
        record.setCreateTime(now);

        return record;
    }

    /*
     * @author: wj
     * @description: 查询时间线
     * @date: 2019/4/8 18:59
     * @param
     * @return
     */
    public List getTimeLine(WfSystaskVO wfSysTaskVO) {
        List<WfSystaskVO> list = wfSystaskVOMapper.selectWfSystaskTimeLine(wfSysTaskVO);
        if (list == null) return Collections.emptyList();
        Set<Integer> set = new HashSet<Integer>();
        Set<Integer> setTodoUser = new HashSet<Integer>();
        for (WfSystaskVO eveWfSysTaskVO : list) {
            String taskDoneUser = eveWfSysTaskVO.getTaskDoneUser();
            String taskTodoUser = eveWfSysTaskVO.getTaskTodoUser();

            if (taskDoneUser != null) {
                String[] doneUsers = taskDoneUser.split(",");
                for (String doneUser : doneUsers) {
                    set.add(Integer.parseInt(doneUser));
                }
            }

            if (taskTodoUser != null) {
                String[] todoUsers = taskTodoUser.split(",");
                for (String todoUser : todoUsers) {
                    setTodoUser.add(Integer.parseInt(todoUser));
                }
            }
        }
        List<Integer> userIdList = new ArrayList<Integer>(set);
        List<SysOrgExtend> userList = sysOrgExtendMapper.selectUserListByIds(userIdList);

        if (userList != null && userList.size() > 0) {
            for (WfSystaskVO eveWfSysTaskVO : list) {
                String taskDoneUser = eveWfSysTaskVO.getTaskDoneUser();
                if (taskDoneUser != null) {
                    StringBuilder sb = new StringBuilder();
                    String[] doneUsers = taskDoneUser.split(",");
                    for (String doneUser : doneUsers) {
                        for (SysOrgExtend eveSysUserVO : userList) {
                            if (Integer.valueOf(doneUser).equals(eveSysUserVO.getId())) {
                                sb.append(eveSysUserVO.getName());
                                sb.append(",");
                                break;
                            }
                        }
                    }
                    String userName = sb.toString();
                    if (userName.endsWith(",")) userName = StringUtils.substringBeforeLast(userName, ",");
                    eveWfSysTaskVO.setUserName(userName);
                }
            }
        }

        List<Integer> userIdListTodo = new ArrayList<Integer>(setTodoUser);
        List<SysOrgExtend> userListTodo = sysOrgExtendMapper.selectUserListByIds(userIdListTodo);

        if (userListTodo != null && userListTodo.size() > 0) {
            for (WfSystaskVO eveWfSysTaskVO : list) {
                String taskTodoUser = eveWfSysTaskVO.getTaskTodoUser();
                if (taskTodoUser != null) {
                    StringBuilder sb = new StringBuilder();
                    String[] todoUsers = taskTodoUser.split(",");
                    for (String todoUser : todoUsers) {
                        for (SysOrgExtend eveSysUserVO : userListTodo) {
                            if (Integer.valueOf(todoUser).equals(eveSysUserVO.getId())) {
                                sb.append(eveSysUserVO.getName());
                                sb.append(",");
                                break;
                            }
                        }
                    }
                    String todoUserName = sb.toString();
                    if (todoUserName.endsWith(",")) todoUserName = StringUtils.substringBeforeLast(todoUserName, ",");
                    eveWfSysTaskVO.setTodoUserName(todoUserName);
                }
            }
        }
        return list;
    }
}
